﻿using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using Color = System.Windows.Media.Color;

namespace LS.DicomViewer.ViewModels
{
    public class ImageThumbnailViewModel : Common.BindableObject<ImageThumbnailViewModel>
    {
        #region 属性/变量   

        string imageFilePath;
        public string ImageFilePath
        {
            get => imageFilePath;
            set => SetProperty(ref imageFilePath, value);
        }

        BitmapSource source = null;
        public BitmapSource ImageSource
        {
            get => source;
            set => SetProperty(ref source, value);
        }

        bool showDrawToolbar = true;
        /// <summary>
        /// 是否显示操作工具栏
        /// </summary>
        public bool ShowDrawToolbar
        {
            get => showDrawToolbar;
            set => SetProperty(ref showDrawToolbar, value);
        }
        #endregion

        public DelegateCommand ShowImageViewerCommand { get; set; }

        #region 方法

        public ImageThumbnailViewModel()
        {
            ShowImageViewerCommand = new DelegateCommand(async () => await ShowImageViewer(), () => File.Exists(ImageFilePath));
        }

        /// <summary>
        /// 从路径中加载影像文件
        /// </summary>
        /// <param name="path"></param>
        public void Load(string path)
        {
            ImageFilePath = path;
            ShowImage();
        }

        public void Clear()
        {
            Dispatcher.Invoke(() => ImageSource = null);
        }

        private void ShowImage()
        {
            if (!File.Exists(ImageFilePath)) return;
            Dispatcher.Invoke(() =>
              {
                  ImageSource = new BitmapImage(new Uri(ImageFilePath, UriKind.Absolute));
              });
        }

        public async Task<ImageViewModel> ShowImageViewer()
        {
            return await Dispatcher.InvokeAsync(() =>
            {
                var view = new ImageViewer();
                var window = new HandyControl.Controls.Window();
                window.Content = view;
                window.Width = 1300;
                window.Height = 800;
                window.Style = (Style)Application.Current.Resources["WindowWin10"];
                window.NonClientAreaBackground = new SolidColorBrush(Color.FromArgb(255, 2, 61, 74));
                window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                window.Owner = Application.Current.MainWindow;
                window.SizeToContent = SizeToContent.Manual;
                window.Title = System.IO.Path.GetFileName(ImageFilePath);
                window.ResizeMode = ResizeMode.CanResize;
                window.ShowInTaskbar = false;
                window.WindowStyle = WindowStyle.SingleBorderWindow;
                view.HorizontalAlignment = HorizontalAlignment.Stretch;
                view.VerticalAlignment = VerticalAlignment.Stretch;

                var vm = view.ViewModel;
                window.Loaded += async (a, b) =>
                {
                    await vm.Load(ImageFilePath);
                };
                window.ShowDialog();
                return vm;
            });
        }

        #endregion
    }
}
